package org.xxpay.manage.config.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.annotation.MethodLog;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.common.vo.DBApplicationConfig;
import org.xxpay.core.entity.MchInfo;
import org.xxpay.core.entity.PayDomain;
import org.xxpay.manage.common.ctrl.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: pangxioayu
 * @date: 20/01/17
 * @description: 支付域名
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/config/pay_domain")
public class PayDomainController extends BaseController {

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayDomain payDomain = getObject(param, PayDomain.class);
        int count = commonService.payDomainService.count(payDomain);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<PayDomain> payDomainList = commonService.payDomainService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), payDomain);
        DBApplicationConfig dbConfig = commonService.sysConfigService.getDBApplicationConfig();
        String payUrl = dbConfig.getConfig4PayUrl();
        List<JSONObject> objects = new LinkedList<>();
        for(PayDomain pd : payDomainList) {
            JSONObject object = (JSONObject) JSON.toJSON(pd);
            object.put("debugGetAlipayUserIdUrl",  payUrl + "/auth/alipay/" + pd.getId());  // 调试地址
            object.put("debugGetAlipayUserIdCodeUrl",  payUrl + "/qrcode_img_get?url=" + (payUrl + "/auth/alipay/" + pd.getId()) + "&width=200&height=200" );  // 调试地址
            object.put("debugOpenAlipayUrl", payUrl + "/auth/alipay/open");  // 调试地址
            object.put("debugOpenAlipayCodeUrl", payUrl + "/qrcode_img_get?url=" + (payUrl + "/auth/alipay/open") + "&width=200&height=200");  // 调试地址
            objects.add(object);
        }
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(objects, count));
    }

    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Integer id = getIntegerRequired(param, "id");
        PayDomain payDomain = commonService.payDomainService.findById(id);
        return ResponseEntity.ok(XxPayResponse.buildSuccess(payDomain));
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayDomain payDomain = getObject(param, PayDomain.class);
        int count = commonService.payDomainService.update(payDomain);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayDomain payDomain = getObject(param, PayDomain.class);
        // 判断记录是否已存在
        PayDomain queryPayDomain = new PayDomain();
        queryPayDomain.setDomainUrl(payDomain.getDomainUrl());
        if(commonService.payDomainService.find(queryPayDomain) != null) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_RECORD_EXISTS));
        }
        // 如果域名类型为2或3
        if(payDomain.getDomainType() == 2 || payDomain.getDomainType() == 3) {
            if(StringUtils.isBlank(payDomain.getAppId()) ||
                    StringUtils.isBlank(payDomain.getAppPrivateKey()) || StringUtils.isBlank(payDomain.getAlipayPublicKey())) {
                return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_PARAM_NOT_FOUND));
            }
        }
        // 入库
        int count = commonService.payDomainService.add(payDomain);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @MethodLog( remark = "删除支付域名" )
    public ResponseEntity<?> delete(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String resourceIds = getStringRequired(param, "ids");
        String[] ids = resourceIds.split(",");
        commonService.payDomainService.batchDelete(ids);
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

}
